package com.shujia.flink.window

import org.apache.flink.streaming.api.scala._
import org.apache.flink.streaming.api.windowing.assigners.{SlidingProcessingTimeWindows, TumblingEventTimeWindows, TumblingProcessingTimeWindows}
import org.apache.flink.streaming.api.windowing.time.Time

object Demo1TimeWIndow {
  val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

  def main(args: Array[String]): Unit = {

    val lineDS: DataStream[String] = env.socketTextStream("master", 8888)

    val kvDS: DataStream[(String, Int)] = lineDS
      .flatMap(_.split(","))
      .map((_, 1))

    /**
      * TumblingProcessingTimeWindows: 处理时间的滚动窗口
      * TumblingEventTimeWindows: 事件时间滚动窗口
      * SlidingProcessingTimeWindows: 处理时间额滑动窗口
      * SlidingEventTimeWindows： 事件时间的滑动窗口
      */

    kvDS
      .keyBy(_._1)
      .window(SlidingProcessingTimeWindows.of(Time.seconds(5), Time.seconds(1)))
      .sum(1)
      .print()


    env.execute()
  }

}
